
// 工具函数
import { Md5 } from 'ts-md5';
import { v4 as uuidv4 } from 'uuid';
import { format } from 'date-fns';
import { useStore } from '@/store'
import { getJSCurrTime } from './time'
import { getRandomCode } from './num'
export const md5 = (str: string) => {
    return Md5.hashStr(str);
}

export const guid = () => {
    return uuidv4().replace(/-/g, '');
};

// 判断是否属性是否存在
export const isDef = (value: any): boolean => {
    return value !== undefined && value !== null && value !== '';
};

// 自动生成条码
export const createCode = (): string => {
    const d = format(new Date(), 'yyMMddHHmmssS');
    return Number(d[0]) + 1 + d.slice(1);
};

// base64转file对象
export const dataURLtoFile = (dataurl: string, filename: string) => {
    let arr: any = dataurl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, { type: mime });
};
// base64转buffer
export const base64ToUint8Array = (base64String: string | any[]) => {
    const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
    const base64 = (base64String + padding)
        .replace(/\-/g, "+")
        .replace(/_/g, "/");

    const rawData = window.atob(base64);
    const outputArray = new Uint8Array(rawData.length);

    for (let i = 0; i < rawData.length; ++i) {
        outputArray[i] = rawData.charCodeAt(i);
    }
    return outputArray;
};